﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_DAL;
using Nova_Common;
using System.Data.Common;
using System.Data;
//xgw 组盘解盘进行修改，去除了与入库明细表中的实际数量、组盘关系；只是组盘物料来源于入库单，组盘数量小于入库单计划数量；
      
public partial class Manage_Package : System.Web.UI.Page
{
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    DataSet ds = null;
    string strSQL = "";
    int iResNo = 0;
    string strResMsg = "";
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }

//    protected void gv_Bind()
//    {
//        HttpCookie WType = Request.Cookies["WType"];

//        strSQL = @"SELECT TAS.PID,TAS.STOCKNO,TAS.MAT_CODE,TBM.MAT_NAME,TAS.NUM,TAS.Act_Num,TAS.SEC_MAT_ID,TBM.S01 FROM dbo.TAddStorage_List AS TAS 
//                            LEFT JOIN TBase_Interface TBI ON TAS.Sec_Mat_ID=TBI.Sec_Mat_ID
//                            LEFT JOIN TBase_Material TBM ON TAS.MAT_CODE=TBM.MAT_CODE 
//                            WHERE TAS.StockNo='" + txt_StorageNo.Text + "' AND (TBI.S01 = '" + WType.Value.ToString().Trim() + "' OR 'FSSC'='" + WType.Value.ToString().Trim() + "')";
//        cmd = dbHelper.GetSqlStringCommond(strSQL);
//        gv.DataSource = dbHelper.ExecuteDataSet(cmd);
//        gv.DataBind();
//    }
    protected void gv_Bind()
    {
        //HttpCookie WType = Request.Cookies["WType"];

        strSQL = @"SELECT TAS.PID,TAS.STOCKNO,TAS.MAT_CODE,TBM.MAT_NAME,TAS.NUM,TAS.Act_Num,TAS.SEC_MAT_ID,TBM.S01 FROM dbo.TAddStorage_List AS TAS 
                            LEFT JOIN TBase_Interface TBI ON TAS.Sec_Mat_ID=TBI.Sec_Mat_ID
                            LEFT JOIN TBase_Material TBM ON TAS.MAT_CODE=TBM.MAT_CODE 
                            WHERE TAS.StockNo='" + txt_StorageNo.Text + "' ";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        gv.DataSource = dbHelper.ExecuteDataSet(cmd);
        gv.DataBind();
    }

    protected void ibtn_Search_Click(object sender, ImageClickEventArgs e)
    {
        gv_Bind();
        lab_Msg.Text = "";
    }

    protected void lbtn_ZP_Click(object sender, EventArgs e)
    {
        LinkButton lbtnZP = (LinkButton)sender;
        string[] sArray = lbtnZP.CommandArgument.ToString().Split('|');
        txt_Mat_Code.Text = sArray[0].ToString();
        HD_SEC_MAT_ID.Value = sArray[1].ToString();
        HD_Mat_Num.Value = sArray[2].ToString();
        HD_Act_Num.Value = sArray[3].ToString();
    }

    /// <summary>
    /// 重置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtn_Reset_Click(object sender, ImageClickEventArgs e)
    {
        txt_Tray_ID.Text = "";
        txt_Mat_Code.Text = "";
        txt_Mat_Num.Text = "";
        HD_SEC_MAT_ID.Value = "";
        lab_Msg.Text = "";
        txt_SQIDS.Text = "";
        HD_Act_Num.Value = "";
        HD_Mat_Num.Value = "";
      
    }

    /// <summary>
    /// 组盘
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtn_ZP_Click(object sender, ImageClickEventArgs e)
    {
        string strTray_ID = "";
        //if (HD_Mat_Num.Value == "")
        //{
        //    lab_Msg.Text = "请从上方列表中选择物料！";
        //    return;
        //}

        if (HD_SEC_MAT_ID.Value == "")
        {
            lab_Msg.Text = "请从上方列表中选择物料！";
            return;
        }
        if (Convert.ToDecimal(HD_Mat_Num.Value) < (Convert.ToDecimal(txt_Mat_Num.Text.Trim()) + Convert.ToDecimal(HD_Act_Num.Value)))
        {
            lab_Msg.Text = "组盘数量不允许超过计划数量！";
            return;
        }
        try
        {
            if (rbtnl_Zone.SelectedValue == "1")
            {

                if (txt_Tray_ID.Text == "")
                {
                    lab_Msg.Text = "托盘号不能为空！";
                    return;
                }

                strSQL = @"SELECT TOP 1 Structure FROM TRelations TRP 
			                    LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
			                    LEFT JOIN TBase_Zone TBZ ON TBZ.Zone_ID = TBP.Zone_ID 
			                    WHERE TRP.TRAY_ID = @TRAY_ID";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, txt_Tray_ID.Text.Trim());
                DataSet ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    strSQL = ds.Tables[0].Rows[0][0].ToString();
                    if (strSQL != "0")
                    {
                        lab_Msg.Text = "该托盘已经在立体库，不允许再次组盘！";
                        return;
                    }
                }
                strTray_ID = txt_Tray_ID.Text.ToString();
            }
            else
            {
                strTray_ID = "H" + ddl_Zone.SelectedValue.ToString() + ddl_Dou.SelectedValue.ToString();               
            }
            cmd = dbHelper.GetStoredProcCommond("SP_PACKAGE_ALL_100X");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTray_ID);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64, Convert.ToInt64(HD_SEC_MAT_ID.Value)); ;
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int64, txt_Mat_Num.Text);
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);

            dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, txt_Mat_Code.Text.Trim());
            dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, txt_StorageNo.Text.Trim());
            dbHelper.AddInParameter(cmd, "@Act_Num", DbType.Decimal, Convert.ToDecimal(txt_Mat_Num.Text.Trim()));

             dbHelper.ExecuteNonQuery(cmd);
            iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strResMsg = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();

            if (iResNo > 0)
            {
                ////谢刚伟注销组盘成功后 入库单明细表实际数量的变更；入库单明细表中TS01字段（组盘关系）的修改
                ////strSQL = @"UPDATE TAddStorage_List SET ACT_NUM=ACT_NUM +'" + txt_Mat_Num.Text.Trim() + "' WHERE STOCKNO=@STOCKNO AND SEC_MAT_ID=@SEC_MAT_ID";
                ////cmd = dbHelper.GetSqlStringCommond(strSQL);
                ////dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, txt_StorageNo.Text.Trim());
                ////dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, HD_SEC_MAT_ID.Value);
                ////dbHelper.ExecuteNonQuery(cmd);

                ////strSQL = @"UPDATE TAddStorage_List SET TS01=(
                ////        SELECT TBS.PID FROM TBase_Storage TBS 
                ////     INNER JOIN TRel_TrayPosContainer TRT ON TRT.CONTAINERID=TBS.CONTAINERID
                ////     INNER JOIN TBase_TrayPos TBT ON TBT.RELATIONS_ID=TRT.RELATIONS_ID WHERE TBT.TRAY_ID=@TRAY_ID AND TBS.SEC_MAT_ID=@SEC_MAT_ID)
                ////        WHERE STOCKNO=@STOCKNO AND SEC_MAT_ID=@SMI";
                ////cmd = dbHelper.GetSqlStringCommond(strSQL);
                ////dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTray_ID);
                ////dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, HD_SEC_MAT_ID.Value);
                ////dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, txt_StorageNo.Text.Trim());
                ////dbHelper.AddInParameter(cmd, "@SMI", DbType.String, HD_SEC_MAT_ID.Value);
                ////dbHelper.ExecuteNonQuery(cmd);

                lab_Msg.Text = "组盘成功！";
                ////更新入库单中的实际的入库数量
                //strSQL = @"UPDATE TAddStorage_List SET Act_Num=Act_Num+@Act_Num where STOCKNO=@STOCKNO and Mat_Code=@Mat_Code and SEC_MAT_ID=@SEC_MAT_ID";
                //                cmd = dbHelper.GetSqlStringCommond(strSQL);
                //                dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, txt_Mat_Code.Text.Trim());
                //                dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64,Convert.ToInt64(HD_SEC_MAT_ID.Value));
                //                dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, txt_StorageNo.Text.Trim());
                //                dbHelper.AddInParameter(cmd, "@Act_Num", DbType.Decimal,Convert.ToDecimal(txt_Mat_Num.Text.Trim()));
                //                dbHelper.ExecuteNonQuery(cmd);


                ////写入或者更新组盘明细表TPackage_tail中，此表用于解盘时入库单明细的货物实际数量的更新

                //strSQL = @"select * from TPackage_tail TPT 
                //           left join TBase_Storage TBS on TPT.PID=TBS.PID
                //           left join TRel_TrayPosContainer TRT on TBS.ContainerID=TRT.ContainerID
                //           left join TBase_TrayPos TBT on TRT.Relations_ID=TBT.Relations_ID
                //           where TBT.Tray_ID=@TRAY_ID and StockNo=@StockNo and Sec_Mat_ID=@Sec_Mat_ID";


                ////strSQL = @"SELECT *  FROM TPackage_tail where TrayID=@TrayID and StockNo=@StockNo and Sec_Mat_ID=@Sec_Mat_ID";
                //cmd = dbHelper.GetSqlStringCommond(strSQL);
                //dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTray_ID);
                //dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, txt_StorageNo.Text.Trim());
                //dbHelper.AddInParameter(cmd, "@Sec_Mat_ID", DbType.Int64, Convert.ToInt64(HD_SEC_MAT_ID.Value));
                //DataSet ds = dbHelper.ExecuteDataSet(cmd);
                //if (ds != null && ds.Tables[0].Rows.Count > 0)
                //{

                //    strSQL = @"update TPackage_tail set Num=Num+@Act_Num from TPackage_tail TPT 
                //              left join TBase_Storage TBS on TPT.PID=TBS.PID
                //              left join TRel_TrayPosContainer TRT on TBS.ContainerID=TRT.ContainerID
                //              left join TBase_TrayPos TBT on TRT.Relations_ID=TBT.Relations_ID
                //              where TBT.TrayPos_ID=@TRAY_ID and StockNo=@StockNo and Sec_Mat_ID=@Sec_Mat_ID";


                //    //strSQL = @"UPDATE TPackage_tail SET Num=Num+@Act_Num where TrayID=@TrayID and StockNo=@StockNo and Sec_Mat_ID=@Sec_Mat_ID";
                //    cmd = dbHelper.GetSqlStringCommond(strSQL);
                //    dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTray_ID);
                //    dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64, Convert.ToInt64(HD_SEC_MAT_ID.Value));
                //    dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, txt_StorageNo.Text.Trim());
                //    dbHelper.AddInParameter(cmd, "@Act_Num", DbType.Decimal, Convert.ToDecimal(txt_Mat_Num.Text.Trim()));
                //    dbHelper.ExecuteNonQuery(cmd);
                //}
                //else
                //{
                //    strSQL = @"insert into TPackage_tail(PID,StockNo,Num) values((select TBS.PID from TBase_Storage TBS left join TRel_TrayPosContainer TRT on TBS.ContainerID=TRT.ContainerID left join TBase_TrayPos TBT on TRT.Relations_ID=TBT.Relations_ID where TBT.Tray_ID=@TRAY_ID and TBS.Sec_Mat_ID=@Sec_Mat_ID ),@StockNo,@Num)";

                //    ////strSQL = @"INSERT INTO TPackage_tail
                //    ////                   (TrayID
                //    ////                   ,StockNo
                //    ////                   ,Sec_Mat_ID
                //    ////                   ,Num)
                //    ////             VALUES
                //    ////                   (@TrayID
                //    ////                   ,@StockNo
                //    ////                   ,@Sec_Mat_ID
                //    ////                   ,@Num) ";

                //    cmd = dbHelper.GetSqlStringCommond(strSQL);
                //    dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTray_ID);
                //    dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, txt_StorageNo.Text.Trim());
                //    dbHelper.AddInParameter(cmd, "@Sec_Mat_ID", DbType.Int64, Convert.ToInt64(HD_SEC_MAT_ID.Value));
                //    dbHelper.AddInParameter(cmd, "@Num", DbType.Decimal, Convert.ToDecimal(txt_Mat_Num.Text.Trim()));
                //    dbHelper.ExecuteNonQuery(cmd);
                //}


                //清空
                txt_Mat_Code.Text = "";
                txt_Mat_Num.Text = "";
                HD_SEC_MAT_ID.Value = "";
                HD_Act_Num.Value = "";
                HD_Mat_Num.Value = "";
                //不能刷新？由于前台的更新模块限制
                gv_Bind();
               
                //xgw 添加注释入库指令按钮（普通立体库不需要）
                //ibtn_RK.Visible = true;
            }
            else
            {
                lab_Msg.Text = strResMsg.ToString();
            }
            
        }
        catch (Exception ex)
        {
            lab_Msg.Text = ex.ToString();
        }

    }

    protected void rbtnl_Zone_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rbtnl_Zone.SelectedValue == "1")
        {
            td_t.Visible = true;
            td_h.Visible = false;
        }
        else
        {
            td_t.Visible = false;
            td_h.Visible = true;
        }
    }
    //有关平库的入库指令(半自动穿梭车系统不需要)
    //protected void ibtn_RK_Click(object sender, ImageClickEventArgs e)
    //{
    //    string strTray_ID = "H" + ddl_Zone.SelectedValue.ToString() + ddl_Dou.SelectedValue.ToString();    
    //    string strPos_ID = Convert.ToString(Convert.ToInt32(ddl_Zone.SelectedValue.ToString()) + 20) + "00000000" + Convert.ToString(ddl_Dou.SelectedValue.ToString());

    //    strSQL = @"UPDATE TRelations SET POS_ID='" + strPos_ID + "' WHERE TRAY_ID='" + strTray_ID + "'";
    //    cmd = dbHelper.GetSqlStringCommond(strSQL);
    //    dbHelper.ExecuteNonQuery(cmd);

    //    strSQL = @"UPDATE TBase_Position SET POS_STATE=1 WHERE POS_ID='" + strPos_ID + "'";
    //    cmd = dbHelper.GetSqlStringCommond(strSQL);
    //    dbHelper.ExecuteNonQuery(cmd);

    //    strSQL = @"INSERT INTO TCommands_HZK(SBH,DH,STATUS) VALUES('" + Convert.ToInt32(ddl_Zone.SelectedValue.ToString()) + "','" + Convert.ToInt32(ddl_Dou.SelectedValue.ToString()) + "',0)";
    //    cmd = dbHelper.GetSqlStringCommond(strSQL);
    //    dbHelper.ExecuteNonQuery(cmd);
    //    ibtn_RK.Visible = false;
    //}


    protected string CheckCode(string strTray_ID)
    {
        string strCode = "";

        strSQL = @"SELECT TBI.S01 FROM TBase_Storage TBS
	                    LEFT JOIN TRel_TrayPosContainer TRT ON TRT.ContainerID=TBS.ContainerID
	                    LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID=TRT.Relations_ID
	                    LEFT JOIN TBase_Interface TBI ON TBI.Sec_Mat_ID=TBS.Sec_Mat_ID
	                    WHERE TBT.Tray_ID='" + strTray_ID + "'";
        cmd = dbHelper.GetSqlStringCommond(strSQL);

        DataSet ds = dbHelper.ExecuteDataSet(cmd);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            strCode = ds.Tables[0].Rows[0][0].ToString();
        }
        return strCode;
    }
    protected void ibtn_RK_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (txt_Tray_ID.Text == "")
            {
                lab_Msg.Text = "托盘号不能为空！";
                return;
            }
            if (txt_SQIDS.Text == "")
            {
                lab_Msg.Text = "输送机号不能为空！";
                return;
            }
            string strUserNo = Request.Cookies["vNovaUserNo"].Value;
            
            cmd = dbHelper.GetStoredProcCommond("SP_Generate_Input_CMDS_By_TRay_ID_Shuttle_FL");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, txt_Tray_ID.Text.Trim());
            //dbHelper.AddInParameter(cmd, "@Area_ID", DbType.Int16, 1);
            dbHelper.AddInParameter(cmd, "@DEFAULT_TYPEOFSIZE", DbType.Int16, 0);
            dbHelper.AddInParameter(cmd, "@DEFAULT_TYPEOFWEIGHT", DbType.Int16, 0);
            dbHelper.AddInParameter(cmd, "@SQIDS", DbType.Int16, txt_SQIDS.Text.Trim());
            dbHelper.AddInParameter(cmd,"@MEMO", DbType.String, 255);
            dbHelper.AddInParameter(cmd, "@User_Name", DbType.String, strUserNo);

            dbHelper.AddOutParameter(cmd, "@TASKID", DbType.Int64, 100);
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strResMsg = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();

            if (iResNo>0)
            {

                lab_Msg.Text = " 入库指令生成！";
            }

            if(iResNo<=0)
            {
                if (strResMsg.Length>0)
                {
                    lab_Msg.Text = strResMsg;

                }
            }

        }
        catch (Exception ex)
        {
            lab_Msg.Text = ex.ToString();
        }

    }
}
